package com.example.everying.framework.websocket.handler;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.everying.framework.websocket.service.WebSocketChatService;
import com.example.everying.model.entity.system.SysNotification;
import com.example.everying.service.system.SysNotificationService;
import jakarta.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class NoticeNumMessageMessageHandler implements MessageHandler

{


    private static SysNotificationService sysNotificationService;
    @Autowired
    private void setService(SysNotificationService sysNotificationService){
        NoticeNumMessageMessageHandler.sysNotificationService = sysNotificationService;
    }

    /**
     * @param session
     * @param message
     */
    @Override
    public void handle(Session session, JSONObject message) {
        String userId = WebSocketChatService.getUserId(session);
        JSONObject  result = new JSONObject();
        result.put("type", "sysNoticeNumMessage");
        result.put("noticeNum", sysNotificationService.count(new QueryWrapper<SysNotification>().eq("recipient_id", userId).eq("msg_status", "2")));
        WebSocketChatService.sendMessageToUserId(userId, result.toJSONString());

    }
}
